package com.faithbj.webservice;

import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.faithbj.common.model.compare.AFMOfficalData;
import com.faithbj.common.model.compare.CivilOfficalData;
import com.faithbj.common.model.compare.HPOfficalData;
import com.faithbj.common.model.compare.IBAOfficalData;
import com.faithbj.common.model.compare.PopulationOfficalData;
import com.faithbj.common.model.compare.SIOfficalData;
import com.faithbj.common.model.compare.TaxOfficalData;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.constant.Constant;
import com.faithbj.service.CompareService;
import com.faithbj.support.vo.AFMCompareResponse;
import com.faithbj.support.vo.CompareRequest;
import com.faithbj.support.vo.CompareRequest.Applicant;

/**
 * Webservice 客户端
 * 
 * @author chen liu
 */
public class WebServiceClient {
    private static ApplicationContext context;
    private static WebServiceServer client;
    private static CompareService compareService;
    static {
        context = new ClassPathXmlApplicationContext(new String[] { "spring/webservice.xml", "spring/beans.xml" });
        client = context.getBean("webServiceClient", WebServiceServer.class);
        compareService = context.getBean(CompareService.class);
    }

    // 发送委办请求
    public static void sendRequest(Map<String, List<CompareRequest>> requestMap) {
        for (String depNum : requestMap.keySet()) {
            boolean result = client.sendRequest(requestMap.get(depNum));
            if (result) {
                System.out.println("查询请求发送成功!");
                saveRequest(depNum, requestMap.get(depNum));
            } else {
                System.out.println("查询请求发送失败!");
            }
        }

    }

    // 委办返回
    public static boolean returnOfficalData(List<AFMCompareResponse> responseList){
        return client.sendResponse(responseList);
    }
    
    private static void saveRequest(String depNum, List<CompareRequest> requestList) {
        System.out.println(depNum);
        if (depNum.equals(Constant.DEP_NUM_AFM)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    AFMOfficalData data = new AFMOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(), "身份证",
                            request.getMainApplicantCardId(), applicant.getApplicantName(), "身份证",
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, DateUtils.getCurrentDate(),
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateAFMOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_CIVIAL)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    CivilOfficalData data = new CivilOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(), "身份证",
                            request.getMainApplicantCardId(), applicant.getApplicantName(), "身份证",
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, DateUtils.getCurrentDate(),
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateCivilOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_HP)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    HPOfficalData data = new HPOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(), "身份证",
                            request.getMainApplicantCardId(), applicant.getApplicantName(), "身份证",
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, DateUtils.getCurrentDate(),
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateHPOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_IBA)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    IBAOfficalData data = new IBAOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(), "身份证",
                            request.getMainApplicantCardId(), applicant.getApplicantName(), "身份证",
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, DateUtils.getCurrentDate(),
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateIBAOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_PS)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    PopulationOfficalData data = new PopulationOfficalData(request.getApplyNum(),
                            request.getBatchNum(), request.getDepartmentNum(), request.getMainApplicantName(), "身份证",
                            request.getMainApplicantCardId(), applicant.getApplicantName(), "身份证",
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, DateUtils.getCurrentDate(),
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdatePSOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_SIO)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    SIOfficalData data = new SIOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(), "身份证",
                            request.getMainApplicantCardId(), applicant.getApplicantName(), "身份证",
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, DateUtils.getCurrentDate(),
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateSIOfficalData(data);
                }
            }
        }
        if (depNum.equals(Constant.DEP_NUM_TAX)) {
            for (CompareRequest request : requestList) {
                for (Applicant applicant : request.getApplicants()) {
                    TaxOfficalData data = new TaxOfficalData(request.getApplyNum(), request.getBatchNum(),
                            request.getDepartmentNum(), request.getMainApplicantName(), "身份证",
                            request.getMainApplicantCardId(), applicant.getApplicantName(), "身份证",
                            applicant.getApplicantCardId(), Constant.INTEGER_FALSE, DateUtils.getCurrentDate(),
                            DateUtils.getCurrentDate(), null);
                    compareService.saveOrUpdateTaxOfficalData(data);
                }
            }
        }

    }
    
}
